package com.mall.controller.portal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallUser;
import com.mall.service.IUserService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.PropertiesUtil;
import com.mall.util.RedisPoolUtil;
/**
 * 用户管理
 * @author ASUS
 *
 */

@Controller
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private IUserService service;
	
	@RequestMapping("sarch/{id}.do")
	@ResponseBody
	public MmallUser getUser(@PathVariable int id){
		
		MmallUser user = service.getUser(id);
		return user;
	}
	
	@RequestMapping(value="login.do",method=RequestMethod.POST)
	@ResponseBody           //用户登录
	public ServerResponse<MmallUser> login(String username, String password,HttpSession session,HttpServletResponse response){
		
		ServerResponse<MmallUser> result = service.login(username, password);
		
		if(result.isSuccess()){
		 	
		   //session.setAttribute(Const.CURRENT_USER,result.getData() );
			
		   CookieUtil.writeLoginToken(response, session.getId());
		   RedisPoolUtil.setEx(session.getId(), JsonUtil.obj2String(result.getData()), Const.Redis.expireTime);
		   
		}
		
		return result;
	}
	
	@RequestMapping(value="logout.do",method=RequestMethod.POST)
	@ResponseBody           //用户注销登录
	public ServerResponse<String> logout(HttpServletRequest request, HttpServletResponse response){
		
		//session.removeAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);
		CookieUtil.delLoginToken(request, response);
		RedisPoolUtil.del(loginToken);
		return ServerResponse.createBySuccess();
	}
	
	@RequestMapping(value="register.do",method=RequestMethod.POST)
	@ResponseBody			//用户注册
	public ServerResponse<String> register(MmallUser user){
		
		return service.register(user);
	}
	
	@RequestMapping(value="check.do",method=RequestMethod.POST)
	@ResponseBody		 	//检测用户名和邮箱是否存在 接口
	public ServerResponse<String> check_valid(String str,String type){
		
		return service.checkValid(str, type);
	}
	
	@RequestMapping(value="getUserInfo.do",method=RequestMethod.POST)
	@ResponseBody			//获取当前登陆的用户的信息
	public ServerResponse<MmallUser> getUserInfo(HttpServletRequest request){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登陆,无法获取用户当前信息");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		if(user!=null){
			return ServerResponse.createBySuccess(user);
		}
		
		return ServerResponse.createByErrorMessage("用户未登陆,无法获取用户当前信息");
	}
	
	@RequestMapping(value="forget_get_question.do",method=RequestMethod.POST)
	@ResponseBody			//忘记问题,通过用户名查找问题,只能post提交,get提交找不到汉字
	public ServerResponse<String> forgetGetQuestion(String username){
		
		return service.selectQuestion(username);
	}
				
	@RequestMapping(value="check_answer.do",method=RequestMethod.POST)
	@ResponseBody			//忘记问题答案,通过用户名和问题,验证答案
	public ServerResponse<String>  checkAnswer(String username, String question,String answer){
		
		return service.checkAnswer(username,question,answer);
		
	} 
	
	@RequestMapping(value="resetPassword.do",method=RequestMethod.POST)
	@ResponseBody			//忘记密码时的重置密码 
	public ServerResponse<String> resetPassword(String username, String newPassword,String forgetToken){
		
		return service.resetPassword(username,newPassword,forgetToken);
	}
	
	@RequestMapping(value="login_reset_password.do",method=RequestMethod.POST)
	@ResponseBody			//用户登录时的重置密码
	public ServerResponse<String> loginResetPassword(HttpServletRequest request,String oldPassword,String newPassword){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		return service.loginResetPassword(oldPassword,newPassword,user);
	}
	
	@RequestMapping(value="updateInfo.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse<MmallUser> updateInformation(HttpServletRequest request,MmallUser user){
		
		//MmallUser curUser = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser curUser = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		
		if(curUser == null){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		user.setId(curUser.getId());
		user.setUsername(curUser.getUsername());
			
		ServerResponse<MmallUser> response = service.updateInformation(user);
		if(response.isSuccess()){
			//用户名不能更改
			response.getData().setUsername(curUser.getUsername());
			//session.setAttribute(Const.CURRENT_USER, response.getData());
			RedisPoolUtil.setEx(loginToken, JsonUtil.obj2String(response.getData()), Const.Redis.expireTime);
		}
		return response;
	}
	
	@RequestMapping("getInformation.do")
	@ResponseBody
	public ServerResponse<MmallUser> getInformation(HttpServletRequest request){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,需要强制登录");
		}
		
		return service.getInformation(user.getId());
	}
	
}
